﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;
using bs_ShowRoom;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Text;
namespace ShowRoom.admin
{
    public partial class AddImgSlide : System.Web.UI.Page
    {
        private const string updMsg = "Update complete!";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetDataEdit();
            }
        }
        private void GetDataEdit()
        {
            if (Request.QueryString["img_id"] != "" && Request.QueryString["img_id"] != null)
            {
                DataTable dt = GrtLogoHopTac(Request.QueryString["img_id"].ToString().Trim(), "", "");
                if (dt.Rows.Count > 0)
                {
                    txtTen.Text = dt.Rows[0]["img_name"].ToString().Trim();
                    ddlActive.SelectedValue = dt.Rows[0]["active"].ToString().Trim();
                    hImagePath.Value = dt.Rows[0]["picture"].ToString().Trim();
                    txtLink.Text = dt.Rows[0]["link_web"].ToString().Trim();
                    txtNote.Value = dt.Rows[0]["note"].ToString().Trim();
                }
            }
        }
        private DataTable GrtLogoHopTac(string img_id, string img_name, string active)
        {
            bs_info mybs = new bs_info(ConfigurationManager.ConnectionStrings["ShowRoomConnectionString"].ToString());

            PccMsg myMsg = new PccMsg();
            myMsg.CreateFirstNode("img_id", img_id);
            myMsg.CreateFirstNode("img_name", img_name);
            myMsg.CreateFirstNode("active", active);
            DataSet ds = mybs.DoReturnDataSet("GETIMGSLIDEADMIN", myMsg.GetXmlStr, "");
            DataTable dt = new DataTable();
            if (ds.Tables.Count > 0)
                dt = ds.Tables[0];

            return dt;

        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            const string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
            const byte LENGTH = 5;
            StringBuilder strCaptcha = new StringBuilder();
            Random rand = new Random();
            for (int i = 0; i < LENGTH; i++)
            {
                string str = chars[rand.Next(chars.Length)].ToString();
                strCaptcha.Append(str);
            }

            string aNum = strCaptcha.ToString();

            if (Page.IsValid == true)
            {
                string ImageUrl = null;
                if ((FileUpload.PostedFile.FileName.Trim().Equals(string.Empty)))
                {
                    ImageUrl = null;
                    if (Request.QueryString["img_id"] == "" && Request.QueryString["img_id"] == null)
                    {
                        Display.Text = "File upload không được rỗng !";
                        return;
                    }
                }
                else
                {
                    string fn = Mupload.CheckUpload(FileUpload.PostedFile.FileName, Mupload.UploadAllowedPicture, FileUpload.PostedFile.ContentLength, 50000000);
                    if (!string.IsNullOrEmpty(fn))
                    {
                        Display.Text = fn;
                        return;
                    }
                    ImageUrl = "slide_" + aNum + (FileUpload.FileName);
                    FileUpload.SaveAs(Server.MapPath("~\\pub\\images\\imgSlide") + "\\" + ImageUrl);
                }

                string img_id = "";
                string myType = "";
                string picture = ImageUrl;
                string link_web = txtLink.Text.ToString().Trim();
                if (Request.QueryString["Type"] != "" && Request.QueryString["Type"] != null)
                {
                    myType = Request.QueryString["Type"].ToString().Trim();
                    if (myType == "Insert")
                    {
                        myType = "0";
                    }
                    else
                    {
                        myType = "1";
                        if (ImageUrl == null)
                            picture = hImagePath.Value.Trim();
                    }
                }

                if (Request.QueryString["img_id"] != "" && Request.QueryString["img_id"] != null)
                {
                    img_id = Request.QueryString["img_id"];
                }
                string user_id = Session["user_id"].ToString().Trim();
                string note = txtNote.Value;
                string img_name = txtTen.Text.ToString().Trim();
                string active = ddlActive.SelectedValue.Trim();
                string myStrReturn = fnInsUpdImg(img_id, img_name, picture, active, link_web, user_id, note, myType);


                if (myStrReturn != "")
                {
                    string url = "";
                    url = "imgSlideManager.aspx";
                    ScriptFunction.ShowMsgTranOpennerUrl_ScriptManager(updMsg, Page, url);
                }
                else
                {
                    ScriptFunction.ShowMsg_ScriptManager(myStrReturn, Page);
                }
            }
        }
        private string fnInsUpdImg(string img_id, string img_name, string picture, string active, string link_web, string user_id,string note, string myType)
        {
            PccMsg myMsg = new PccMsg();
            myMsg.CreateFirstNode("img_id", img_id);
            myMsg.CreateFirstNode("img_name", img_name);
            myMsg.CreateFirstNode("picture", picture);
            myMsg.CreateFirstNode("active", active);
            myMsg.CreateFirstNode("link_web", link_web);
            myMsg.CreateFirstNode("user_id", user_id);
            myMsg.CreateFirstNode("note", note);
            myMsg.CreateFirstNode("Type", myType);

            bs_info mybs = new bs_info(ConfigurationManager.ConnectionStrings["ShowRoomConnectionString"].ToString());

            string strXML = myMsg.GetXmlStr;
            string strReturn = mybs.DoReturnStr("INSUPDIMGSLIDE", strXML, "");
            return strReturn;
            //*****************************************************************************
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Write("<script>window.close();</script>");
        }
    }
}